package admin.server.controller;


import admin.server.service.UmsMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import admin.common.vo.PageVo;
import admin.common.vo.ResponseVO;
import admin.server.model.dto.UmsMenuNodeDto;
import admin.dao.model.UmsMenu;

import java.util.List;

/**
 * 后台菜单管理Controller
 * Created by zgb on 2020/2/4.
 */
@Controller
@Tag(name = "后台菜单管理")
@RequestMapping("/menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService menuService;

    @Operation(summary = "添加后台菜单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO create(@RequestBody UmsMenu umsMenu) throws Exception {
        menuService.create(umsMenu);
        return ResponseVO.success();
    }

    @Operation(summary = "修改后台菜单")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO update(@PathVariable Long id,
                               @RequestBody UmsMenu umsMenu) throws Exception {
        int count = menuService.update(id, umsMenu);
        if (count > 0) {
            return ResponseVO.success(count);
        } else {
            return ResponseVO.failed();
        }
    }

    @Operation(summary = "根据ID获取菜单详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<UmsMenu> getItem(@PathVariable Long id) {
        UmsMenu umsMenu = menuService.getItem(id);
        return ResponseVO.success(umsMenu);
    }

    @Operation(summary = "根据ID删除后台菜单")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO delete(@PathVariable Long id) {
        menuService.delete(id);
        return ResponseVO.success();
    }

    @Operation(summary = "分页查询后台菜单")
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<PageVo<UmsMenu>> list(@PathVariable Long parentId,
                                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageVo<UmsMenu> page = menuService.list(parentId, pageSize, pageNum);
        return ResponseVO.success(page);
    }


    @Operation(summary = "列表查询后台菜单")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<List<UmsMenu>> list(@RequestParam(value = "level", required = false) Integer level) {
        List<UmsMenu> menuList = menuService.list(level);
        return ResponseVO.success(menuList);
    }

    @Operation(summary = "列表查询后台非叶子菜单")
    @RequestMapping(value = "/noLeafList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<List<UmsMenu>> noLeafList() {
        return ResponseVO.success(menuService.noLeafList());
    }

    @Operation(summary = "列表查询后台叶子菜单")
    @RequestMapping(value = "/leafList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<List<UmsMenu>> leafList() {
        return ResponseVO.success(menuService.leafList());
    }


    @Operation(summary = "树形结构返回所有菜单列表")
    @RequestMapping(value = "/treeList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<List<UmsMenuNodeDto>> treeList() {
        List<UmsMenuNodeDto> list = menuService.treeList(false);
        return ResponseVO.success(list);
    }

    @Operation(summary = "树形结构返回所有菜单-资源列表")
    @RequestMapping(value = "/treeWithResourceList", method = RequestMethod.GET)
    @ResponseBody
    public ResponseVO<List<UmsMenuNodeDto>> treeWithResourceList() {
        List<UmsMenuNodeDto> list = menuService.treeList(true);
        return ResponseVO.success(list);
    }

    @Operation(summary = "修改菜单显示状态")
    @RequestMapping(value = "/updateHidden/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVO updateHidden(@PathVariable Long id, @RequestParam("hidden") Boolean hidden) {
        int count = menuService.updateHidden(id, hidden);
        if (count > 0) {
            return ResponseVO.success(count);
        } else {
            return ResponseVO.failed();
        }
    }
}
